package com.ruoyi.stock.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stock.domain.StockHold;
import com.ruoyi.stock.domain.StockUser;
import com.ruoyi.stock.service.IStockHoldService;
import com.ruoyi.stock.service.IStockInqueryService;
import com.ruoyi.stock.service.IStockUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 持仓记录
Controller
 * 
 * @author xxb
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/stock/stockhold")
@Api( tags = "持仓记录")
public class StockHoldController extends BaseController
{
    @Autowired
    private IStockHoldService stockHoldService;

    @Autowired
    private IStockUserService stockUserService;

    @Autowired
    private IStockInqueryService stockInqueryService;




    /**
     * 查询持仓记录列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "持仓录入查询")
    public TableDataInfo list(StockHold stockHold)
    {
        startPage();
        List<StockHold> list = stockHoldService.selectStockHoldList(stockHold);
        return getDataTable(list);
    }

    /**
     * 小程序查询持仓记录列表
     */
    @GetMapping("/listapp")
    @ApiOperation(value = "小程序持仓录入查询")
    public TableDataInfo listapp(StockHold stockHold)
    {
       // List<StockInquery> resultList  = new ArrayList<>();
        stockHold.setSysUserId(SecurityUtils.getUserId());
        startPage();
        List<StockHold> list = stockHoldService.selectStockHoldList(stockHold);
//        for(StockHold stockhold1: list){
//            StockInquery stockInquery=new StockInquery();
//            stockInquery.setSecurityCode(stockhold1.getSecurityCode());
//            List<StockInquery> list2 =stockInqueryService.selectStockInqueryList(stockInquery);
//            resultList.addAll(list2);
//        }
        return getDataTable(list);
    }

    /**
     * 导出持仓记录列表
     */
    @Log(title = "持仓记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockHold stockHold)
    {
        List<StockHold> list = stockHoldService.selectStockHoldList(stockHold);
        ExcelUtil<StockHold> util = new ExcelUtil<StockHold>(StockHold.class);
        util.exportExcel(response, list, "持仓记录 数据");
    }

    /**
     * 获取持仓记录
详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stockHoldService.selectStockHoldById(id));
    }

    /**
     * 新增持仓记录
     */
    @Log(title = "持仓记录", businessType = BusinessType.INSERT)
    @ApiOperation(value = "持仓录入")
    @PostMapping
    public AjaxResult add(@RequestBody StockHold stockHold)
    {
        stockHold.setSysUserId(SecurityUtils.getUserId());
        StockUser stockUser=new StockUser();
        stockUser.setSysUserid(SecurityUtils.getUserId());
        List<StockUser> list= stockUserService.selectStockUserList(stockUser);
        if(list.size()>0){
            stockHold.setCreateBy(list.get(0).getUserName());
        }
        return toAjax(stockHoldService.insertStockHold(stockHold));
    }

    /**
     * 修改持仓记录

     */
    @Log(title = "持仓记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockHold stockHold)
    {
        return toAjax(stockHoldService.updateStockHold(stockHold));
    }

    /**
     * 删除持仓记录

     */
    @Log(title = "持仓记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockHoldService.deleteStockHoldByIds(ids));
    }
}
